import request from './request';
import { assert, setOptions } from './common';
import defaultOptions from './default'


// constructor 里可以返回一个实例  
class Axios {
  constructor() {            // this  axios 实例
    let _this = this
    return new Proxy(request, {
      get(data, name) {
        return _this[name];
      },
      set(data, name, value) {
        _this[name] = value;
        return true;
      },
      apply(fn, thisArg, args) {
        let options = _this._preprocessArgs('', args);
        if(!options) {
          if(args.length === 2) {
            assert(typeof args[0] === 'string', 'args[0] must is string');
            assert(typeof args[1] === 'object' && args[1] && args[1].constructor === Object, 'args[1] must is json');
            
            options = {
              ...args[1],
              url: args[0]
            }
          } else {
            assert(0, "invaild args");
          }
        }
        console.log(options)
      }
    })
  }

  request(options) {
    // 1. 和 this.default 进行合并
    // 2. 检测参数是否正确
    // 3. 正式调用 request(options)
  }

  _preprocessArgs(method, args) {
    let options;
    if(args.length === 1 && typeof args[0] === 'string') {
      options = {method, url: args[0]};
    } else if(args.length === 1 && args[0].constructor === Object) {
      options = {
        ...args[0],
        method
      }
    } else {
      return undefined;
    }

    return options;
  }

  get(...args) {
    let options = this._preprocessArgs('get', args);
    if(!options) {
      if(args.length === 2) {
        assert(typeof args[0] === 'string', 'args[0] must is string');
        assert(typeof args[1] === 'object' && args[1] && args[1].constructor === Object, 'args[1] must is json');
        
        options = {
          ...args[1],
          url: args[0],
          method: 'get'
        }
      } else {
        assert(0, "invaild args");
      }
    }

    console.log(options)
  }

  post(...args) {
    let options = this._preprocessArgs('post', args);
    if(!options) {
      if(args.length === 2) {
        assert(typeof args[0] === 'string', 'args[0] must is string');

        options = {
          data: args[1],
          url: args[0],
          method: 'post'
        }
      } else if(args.length === 3) {
        assert(typeof args[2] === 'object' && args[2] && args[2].constructor === Object, 'args[2] must is json');
        
        options = {
          ...args[2],
          url: args[0],
          data: args[1],
          method: 'post'
        }
      } else {

        assert(false)
      }
    }
    console.log(options)
  }
  delete(...args) {
    let options = this._preprocessArgs('delete', args);
    if(!options) {
      if(args.length === 2) {
        assert(typeof args[0] === 'string', 'args[0] must is string');
        assert(typeof args[1] === 'object' && args[1] && args[1].constructor === Object, 'args[1] must is json');
      
        options = {
          ...args[1],
          url: args[0],
          method: 'delete'
        }
      } else {
        assert(false, 'invalid args');
      }
    }
    console.log(options)
  }
}

Axios.create = Axios.prototype.create = function(options) {
  let axios = new Axios();

  let res = {...JSON.parse(JSON.stringify(defaultOptions))};

  setOptions(res, options)

  axios.default = res;
  return axios;
}

// let axios = new Axios();    // console.log(axios)         proxy实例

export default Axios.create();
